#include "VLua.h"
#include <QProcess>
#include <QDebug>

#include "VFunctions.h"
#include "VGlobal.h"

VLua::VLua()
{

}

QString VLua::run(QString text)
{
    // 保存文件
    VFunctions::save_file(VGlobal::temp_dir + "/1.lua", text);

    // 执行
    QProcess p;
    p.start("\"" + VGlobal::lua_path + "\"", QStringList()
        << VGlobal::temp_dir + "/1.lua");

    qDebug() << "等待执行";
    if (p.waitForStarted() == false)
    {
        qDebug() << "启动失败";
    }
    p.waitForFinished();
    qDebug() << "ok!";

    QString output = QString::fromUtf8(p.readAllStandardOutput());
    QString error = QString::fromUtf8(p.readAllStandardError());

    if(error.length() != 0)
    {
        qDebug() << "lua error:" << error;
    }

    return output;
}
